<?php
load_info_var($cf_controller);	   //加载smarty变量
load_file('brand_class', LIB_PATH);
$main = new Brand($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;
require_once(INFO_PATH.'cf_brand_cate.php');
switch ($cf_action) {
    case "index":
	$article_cate = add_array_item(array('' => '全部'), $article_cate);
	$smarty->mypage($cf_controller, $cf_action, 0);	
	$skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode($_GET['skey']);
	$skey2 = isset($_POST['skey2']) ? trim($_POST['skey2']) : urldecode($_GET['skey2']);
	$skey3 = isset($_POST['skey3']) ? trim($_POST['skey3']) : urldecode($_GET['skey3']);
		
	$where = '1';
	if ($skey) {
	    $where .= " and brand_name like '%" . $skey . "%'";
	}	
	if ($skey2 && $skey3) {
            $where .= " AND DATE_FORMAT(brand_create_time,'%Y-%m-%d')  BETWEEN '".$skey2."' AND '".$skey3."'";
    }
	
	load_file('page_class', LIB_PATH);
	$MyPage = new Mypage($db);
	$field = '*';
	$data_ary = $MyPage->from_sql($field, $main->get_table_name(), $where, "brand_id DESC", intval($_GET['cpage']), "20");
	$page = $MyPage->get_page($data, $cf_self_page . '?dpt=' . $cf_dpt . '&skey=' . $skey . '&skey2=' . $skey2 . '&skey3=' . $skey3);
	

	break;
    case "add":
	if ($cf_ispost) {
		$data = $db->getFormData($main->get_table_name(),$_POST);	
		$data['brand_create_time'] = date("Y-m-d H:i:s");
	    $result = $main->insert_data($data);		
	    if ($result) {
			$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
			unset($_SESSION['pre_url']);
			$more = $_SERVER['HTTP_REFERER'] ? '<a onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a>' : '';
			my_jump('添加成功' . $more, '', $return_page, 3);
	    } else {
			my_jump('添加失败');
	    }
	} else {

	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
		
	}

	break;
    case "edit":
	if ($cf_ispost) {
		$id = intval($_POST['id']);
	    $data = $db->getFormData($main->get_table_name(),$_POST);		
		$data['brand_edit_time'] = date("Y-m-d H:i:s");
		//$db->debug(1,1);				
	    $result = $main->update_data($data, "brand_id='".$id."'");
		
	    if ($result) {
			//更新商品的显示状态
			load_file('goods_class',LIB_PATH);
			$goods = new Goods($db);
			$data = array('goods_status'=>$data['brand_is_show']);
			$where = "goods_brand_id = '".$id."'";			
			$goods->update_data($data,$where);	
						
			$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
			unset($_SESSION['pre_url']);
		    my_success('修改成功', '', $return_page, 3);
	    } else {
		    my_error('修改失败');
	    }
	} else {		
	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
		$row = $main->get_data_row($id);
		if($row['brand_tags']){
			$tags_ary = explode(',',$row['brand_tags']);
			$row['brand_tags'] = '';
			foreach($tags_ary as $v){
				$row['brand_tags'] .= "'".$v."',";				
			}
			trim($row['brand_tags'],',');
			
		}
		//$row['article_content'] = htmlspecialchars_decode($row['article_content'], ENT_QUOTES);
		//dump($row);
	    if (!$row)
		my_jump('沒有此信息');
	
		
		
		
		//dump($row);
	    //my_print($row['item']);
	   
	}
	break;
    case "del":
	$result = $main->del_data($id);
	if ($result) {
	    my_jump('删除成功', '', $return_page, 1);
	} else {
	    my_jump('删除失败!');
	}

	break;
	case "detail":
	$smarty->mypage($cf_controller, $cf_action, 0);
	$row = $main->get_data_row($id);
	if (!$row)
	my_jump('沒有此信息');
	
	$row['brand_content'] = htmlspecialchars_decode($row['brand_content']);
	break;  
	default:
	my_jump('非法操作!');
	break;
}


$smarty->assign(array(
    'id' => $id,
    'row' => $row,
    'data_ary' => $data_ary,	
    'page' => $page,   
    'skey' => $skey,
    'skey2' => $skey2,
    'skey3' => $skey3,
	'brand_cate' => $brand_cate,
));

$smarty->mydisplay();
?>